package mapper

import (
	"github.com/beego/beego/v2/client/orm"
	context2 "golang.org/x/net/context"
	"ksd-social-api/commons/page"
	cmodel "ksd-social-api/modules/resume/model"
	"strings"
)

/**
 * 用户购买小册
 * @author feige
 * @date 2023-11-23
 * @version 1.0
 * @desc
 */
type UserBuyResumeTemplateMapper struct {
}

/**
 * @author feige
 * @date 2023-10-10
 * @version 1.0
 * @desc  保存用户购买小册信息
 */
func (service *UserBuyResumeTemplateMapper) SaveUserBuyResumeTemplate(userBuyResumeTemplate cmodel.UserBuyResumeTemplate) bool {
	// 创建orm对象
	err := orm.NewOrm().DoTx(func(ctx context2.Context, txOrm orm.TxOrmer) (err error) {
		// 保存用户小册订单
		_, err = txOrm.Insert(&userBuyResumeTemplate)
		// 同步购买和订阅的用户数量
		_, err = txOrm.Raw("update xk_resume_template set buy_num = buy_num + 1  where id = ? and buy_num >= 0 and system_id = ?", userBuyResumeTemplate.Opid, userBuyResumeTemplate.SystemId).Exec()
		return err
	})
	return err == nil
}

/**
 * 查询用户是否购买小册
 * @author feige
 * @date 2023-11-23
 * @version 1.0
 * @desc
 */
func (service *UserBuyResumeTemplateMapper) CountUserBuyResumeTemplateNo(userId uint64, templateId uint64, systemId uint64) int {
	// 创建orm对象
	mysql := orm.NewOrm()
	var total int
	err := mysql.Raw("SELECT count(1) FROM xk_resume_template_user_buy WHERE user_id  = ? AND opid = ? and system_id = ?", userId, templateId, systemId).QueryRow(&total)
	// 返回
	if nil != err {
		return 0
	}
	return total
}

/**
 * 统计小册购买人数
 * @author feige
 * @date 2023-11-23
 * @version 1.0
 * @desc
 */
func (service *UserBuyResumeTemplateMapper) CountUserBuyResumeTemplateId(templateId uint64, systemId uint64) int {
	// 创建orm对象
	mysql := orm.NewOrm()
	var total int
	err := mysql.Raw("SELECT count(1) FROM xk_template_user_buy WHERE opid = ? and system_id = ?", templateId, systemId).QueryRow(&total)
	// 返回
	if nil != err {
		return 0
	}
	return total
}

/**
 * @author feige
 * @date 2023-10-12
 * @version 1.0
 * @desc 我学习的小册
 */
func (mapper *UserBuyResumeTemplateMapper) FindMyResumeTemplatesOrderPage(userId uint64, channelIds string, systemId uint64, pageNo int64, pageSize int64) (p *page.Page, err error) {
	// 准备容器对象，开始装载数据库数据
	userBuyResumeTemplateList := []cmodel.UserBuyResumeTemplate{}
	//创建orm对象
	mysql := orm.NewOrm()
	qs := mysql.QueryTable("xk_template_user_buy")

	var total int64 = 0
	query := qs.Filter("user_id", userId).Filter("system_id", systemId)
	if len(channelIds) > 0 {
		split := strings.Split(channelIds, ",")
		query = query.Filter("channel_id__in", split)
	}

	total, _ = query.Count()
	// 重新换算分页和规则
	page := p.Page(pageNo, pageSize, total)
	// 分页查询
	_, err2 := query.All(&userBuyResumeTemplateList)
	// 执行count查询
	if err2 != nil {
		return nil, err
	}
	// 6：把查询的数据放入到分页的records字段，准备返回
	page.Records = userBuyResumeTemplateList
	// 7: 如果没有找到直接返回
	if err2 != nil {
		return nil, err2
	}
	return page, nil
}

/*
*
统计用户已经支付小册的金额
*/
func (mapper *UserBuyResumeTemplateMapper) SumUserByResumeTemplate(specailId uint64, userId uint64, systemId uint64) float64 {
	var totalPrice float64
	err := orm.NewOrm().Raw("SELECT SUM(price) FROM xk_template_user_buy WHERE user_id = ? AND special_id = ? and system_id = ?", userId, specailId, systemId).QueryRow(&totalPrice)
	if err != nil {
		return 0
	}
	return totalPrice
}
